<?php include '../public/header.php' ?>
<?php include '../public/header_public.php' ?>
<style>
    .bkuai{width: 140px;height: 150px;margin-right: 20px;float: left;cursor: pointer;position: relative;border-radius: 5px}
    .bkuai:hover{background-color: #eee;}
    .bkuai:hover .opts{display:block}
    .bkuai img{margin: 10px 30px 0}
    .bkuai .baotit{padding: 0 5px;line-height: 18px;text-align: center;
        display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 3;overflow: hidden;
    }
    .bkuai .opts{position: absolute;right: 0;top: 6px;font-size: 14px;color: #0E0EFF;width: 30px;height: 30px;display: none;}
    .bkuai .optlist{position: absolute;right: 13px;top: 25px;background-color: #FFFFFF;border: 1px solid #eeeeee;
        border-radius: 3px;padding: 5px 0;z-index: 99;
    }
    .bkuai .optlist div{margin-top: 5px;padding: 0 20px}
    .bkuai .optlist div:hover{color: #0E0EFF;}
    #kongjia{text-align: center;color: #bbb;margin-top: 100px;}
    #gofan{position: absolute;left: 0;top: 1px;font-weight: bold;color: red;cursor: pointer;font-size: 18px;padding: 2px 4px}
</style>
<body class="laytp-container">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title">文件资料库</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>

<div class="layui-card" id="search-form">
    <div class="layui-card-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-row">
<!--                    <div class="layui-col-md3" style="width: 10px!important;">-->
<!--                        <div id="gofan" pid="0" title="上一级文件夹">↑</div>-->
<!--                    </div>-->
<!--                    <div class="layui-col-md3" style="margin-left: 0!important;padding-left: 5px!important;">-->
<!--                        <div class="layui-input-block" style="margin-left: 0">-->
<!--                            <input type="text" id="muid_name" placeholder="当前位置" readonly class="layui-input" autocomplete="off" style="background-color: #fafafa">-->
<!--                        </div>-->
<!--                    </div>-->
                    <div class="layui-col-md3">
                        <label class="layui-form-label">文件名</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md3">
                        <label class="layui-form-label">上传人</label>
                        <div class="layui-input-block">
                            <input type="text" name="created_user_name" id="created_user_name" placeholder="请输入" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md3 search_hide none">
                        <label class="layui-form-label">上传时间</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="created_at" id="created_at" placeholder="请输入" autocomplete="off" class="layui-input bdate_sea_range_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md3">
                        <div class="layui-form-item search-div">
                            <div class="layui-form-item layui-inline">
                                <input type="hidden" id="muid" name="muid">
                                <button class="laytp-btn laytp-btn-md laytp-btn-primary" onclick="TableSearchFunc()" type="button"><i class="layui-icon layui-icon-search"></i>查询</button>
                                <button class="layui-btn layui-btn-primary" onclick="listTableResetFunc()" type="button" ><i class="layui-icon layui-icon-refresh"></i>重置</button>
<!--                                <div id="search_open" onclick="toggle_search()">-->
<!--                                    <i class="layui-icon layui-icon-down"></i>展开-->
<!--                                </div>-->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<input type="hidden" id="copyid" name="copyid">
<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <?php include 'mu.php' ?>
            <div class="layui-col-md9" id="table_list">
                <div class="layui-card-body" style="padding:10px">
                    <div>
                        <div class="layui-table-tool-temp">
                            <div class="layui-btn-container">
                                <button class="layui-btn layui-btn-sm laytp-container-tool-button layui-hide" id="but_addjia" onclick="auth_but_func_addjia('创建文件夹')" style="padding-right: 12px;"><i class="layui-icon layui-icon-add-1"></i>创建文件夹</button>
                                <button class="layui-btn layui-btn-sm laytp-container-tool-button layui-hide" id="but_addbao" onclick="auth_but_func_addbao('上传文件')" style="padding-right: 12px;"><i class="layui-icon layui-icon-add-1"></i>上传文件</button>
                                <button class="layui-btn layui-btn-sm laytp-container-tool-button layui-hide" id="but_guanjia" onclick="auth_but_func_guanjia('管理文件夹')" style="padding-right: 12px;">管理文件夹</button>
                            </div>
                        </div>
                    </div>
                    <div id="kuainei">
<!--                        <div class="bkuai">-->
<!--                            <img class="loggin_logo" src="/static/admin/images/bao/jia.png">-->
<!--                            <div class="baotit">asdasd</div>-->
<!--                            <div class="opts"><b>···</b></div>-->
<!--                            <div class="optlist">-->
<!--                                <div>打开</div>-->
<!--                                <div>下载</div>-->
<!--                                <div>编辑</div>-->
<!--                                <div>删除</div>-->
<!--                                <div>详细信息</div>-->
<!--                            </div>-->
<!--                        </div>-->
                    </div>
                    <div id="kongjia" class="layui-hide">未查询到文件信息</div>
                </div>
            </div>
        </div>
    </div>
    <img src="" alt="" id="nanimgs" style="display: none">
    <input type="file" id="uploadfile" multiple style="display: none"/>
</div>
<script>

    $("#gofan").click(function () {
       var pid = $(this).attr('pid') || 0;
        get_mu_info(pid);
        get_jia_info(pid);
    });


    var param_enginid = getQueryVariable('id') || 0;
    layui.use(["laytp",'laypage','table','dropdown','form','soulTable'], function () {
        var laypage = layui.laypage, table = layui.table, dropdown = layui.dropdown, form = layui.form, soulTable = layui.soulTable;
        get_jia_info();
        $("#uploadfile").change(function () {
            var loading = layui.layer.load(0, {shade: 0.1});
            let fd = new FormData();
            for (var i = 0; i < this.files.length; i++) {
                fd.append('files[]', this.files[i]);
            }
            fd.append('token', facade.getCookie('token'));
            fd.append('enginid', param_enginid);
            fd.append('muid', $("#muid").val() || 0);
            fd.append('isadd', 1);
            $.ajax({
                url: server_api_name+ask_engin_bao_add,
                type: "POST",
                data: fd,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function (res) {
                    $("#uploadfile").val();
                    var muid = $("#muid").val();
                    get_mu_info(muid);
                    get_jia_info(muid);
                    layui.layer.close(loading);
                    if (res.code === 2000) {
                        facade.success("提交成功!");
                        setTimeout(function () {
                            searchTableFunc(form, table)
                        },1500)
                    }else if (res.code === 4444) {
                        searchTableFunc(form, table)
                        layer.confirm("<div class='tit'>"+res.msg+"</div><div class='exp'></div>", {
                                title:'操作确认',
                                icon: 3,
                                area:['450px','220px'],
                                btn:['确定']
                            }
                        )

                    }else{
                        facade.error(res.msg);
                    }
                },
                error: function (res) {
                    $("#uploadfile").val();
                    layui.layer.close(loading);
                    var muid = $("#muid").val();
                    get_mu_info(muid);
                    get_jia_info(muid);
                    facade.error("网络错误");
                },
            })
        });
    });

    function get_jia_info(muid=0, type=0){
        var name = $("#name").val() || '';
        var created_user_name = $("#created_user_name").val() || '';
        var created_at = $("#created_at").val() || '';

        facade.ajax({url: ask_engin_baoku_list, data:{
            enginid:param_enginid,
            muid: muid,
            type:type,
            name:name,
            created_user_name:created_user_name,
            created_at:created_at,
        }}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#kuainei").html('');

                var butauth = data.butauth;
                for (var key in butauth) {
                    if($("#but_"+butauth[key].butname).length > 0){
                        $("#but_"+butauth[key].butname).removeClass('layui-hide');
                    }
                }



                var muStr = '';
                var muList = data.muList;
                for (var key in muList){
                    muStr += '<div class="bkuai kuaijia" muid="'+muList[key].id+'">';
                    muStr += '<img class="loggin_logo" src="/static/admin/images/bao/jia.png">';
                    muStr += '<div class="baotit">'+muList[key].name+'</div>';
                    muStr += '<div class="opts"><b>···</b></div>';
                    muStr += '<div class="optlist layui-hide">';
                    muStr += '<div onclick="auth_but_func_openjia('+muList[key].id+')">打开</div>';
                    for (var mmm in butauth) {
                        if(butauth[mmm].butname == 'down'){
                            muStr += '<div onclick="auth_but_func_downjia('+muList[key].id+')">下载</div>';
                        }
                        if(butauth[mmm].butname == 'editjia'){
                            muStr += '<div onclick="auth_but_func_editjia('+muList[key].id+')">编辑</div>';
                        }
                        if(butauth[mmm].butname == 'copy'){
                            muStr += '<div onclick="auth_but_func_copy('+muList[key].id+')">复制</div>';
                            muStr += '<div class="pastemubut layui-hide" onclick="auth_but_func_paste('+muList[key].id+')">粘贴</div>';
                        }
                        if(butauth[mmm].butname == 'del'){
                            muStr += '<div onclick="auth_but_func_deljia('+muList[key].id+')">删除</div>';
                        }
                    }
                    muStr += '<div onclick="auth_but_func_showjia('+muList[key].id+')">详细信息</div>';
                    muStr += '</div>';
                    muStr += '</div>';
                }
                $("#kuainei").append(muStr);


                var baoStr = '';
                var baoList = data.baoList;
                for (var key in baoList){
                    baoStr += '<div class="bkuai kuaibao" ext="'+baoList[key].ext+'" encpath="'+baoList[key].encpath+'">';
                    baoStr += '<img class="loggin_logo" src="/static/admin/images/bao/'+baoList[key].ext+'.png" onerror="this.src=\'/static/admin/images/bao/tongyong.png\'">';
                    baoStr += '<div class="baotit">'+baoList[key].name+'</div>';
                    baoStr += '<div class="opts"><b>···</b></div>';
                    baoStr += '<div class="optlist layui-hide">';
                    baoStr += '<div onclick="auth_but_func_openbao(\''+baoList[key].ext+'\',\''+baoList[key].encpath+'\')">预览</div>';
                    for (var mmm in butauth) {
                        if(butauth[mmm].butname == 'down'){
                            baoStr += '<div onclick="auth_but_func_downbao(\''+baoList[key].encpath+'\')">下载</div>';
                        }
                        if(butauth[mmm].butname == 'editbao'){
                            baoStr += '<div onclick="auth_but_func_editbao('+baoList[key].id+')">编辑</div>';
                        }
                        if(butauth[mmm].butname == 'del'){
                            baoStr += '<div onclick="auth_but_func_delbao('+baoList[key].id+')">删除</div>';
                        }
                    }
                    baoStr += '<div onclick="auth_but_func_showbao('+baoList[key].id+')">详细信息</div>';
                    baoStr += '</div>';
                    baoStr += '</div>';
                }
                $("#kuainei").append(baoStr);



                if(muList.length > 0 || baoList.length > 0){
                    $("#kongjia").addClass('layui-hide');
                }else{
                    $("#kongjia").removeClass('layui-hide');
                }



                var copyid = $("#copyid").val();
                if(copyid){
                    $(".pastemubut").removeClass('layui-hide');
                    $(".optlist").addClass('layui-hide');
                }









                $(".opts").click(function (event) {
                    $(".optlist").addClass('layui-hide');
                    event = event || window.event;
                    $(this).parent('.bkuai').find('.optlist').removeClass('layui-hide');
                    $(document).click(function () {
                        $(".optlist").addClass('layui-hide');
                    });
                    $(".optlist").click(function (e) {
                        e.stopPropagation();
                    });
                    event.stopPropagation();
                    return false;
                });
                $(".kuaijia").dblclick(function (event) {
                    var muid = $(this).attr('muid');
                    auth_but_func_openjia(muid)
                });
                $(".kuaibao").dblclick(function (event) {
                    var ext = $(this).attr('ext');
                    var encpath = $(this).attr('encpath');
                    get_yulan_bao_file(ext, encpath)
                });

                render_from();
            }
        });
    }
    
    
    function auth_but_func_guanjia() {
        openPageEdit('guanmu.php?close=1&enginid='+param_enginid);
    }



    function auth_but_func_addjia(buttitle){
        var muid = $("#muid").val();
        openPageEdit('addmu.php?close=1&muid='+muid+'&enginid='+param_enginid, '1000px', '690px');
    }
    function auth_but_func_openjia(muid){
        choose_muid(muid);
        get_jia_info(muid)
    }
    function auth_but_func_downjia(muid){
        facade.ajax({url: ask_engin_mu_down, data: {muid:muid,enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                down_file(res.data);
            }
        });
    }
    function auth_but_func_editjia(muid){
        openPageEdit('editmu.php?close=1&id='+muid, '1000px', '690px');
    }
    function auth_but_func_copy(muid){
        $("#copyid").val(muid);
        $(".pastemubut").removeClass('layui-hide');
        $(".optlist").addClass('layui-hide');
    }
    function auth_but_func_paste(pasteid){
        var copyid = $("#copyid").val() || 0;
        facade.ajax({type: 'post', url: ask_engin_mu_paste, data:{enginid: param_enginid,pasteid:pasteid,copyid:copyid}}).done(function(res){
            if (res.code === 2000) {
                facade.success("提交成功!");
                setTimeout(() => {
                    try {
                        $("#copyid").val('');
                        $(".pastemubut").addClass('layui-hide');
                        $(".optlist").removeClass('layui-hide');

                        var muid = $("#muid").val() || 0;
                        get_mu_info(muid);
                        get_jia_info(muid);
                    }catch (e) {

                    }
                }, 1500);
            }
        });
    }
    function auth_but_func_deljia(muid){
        data_delete_cur(ask_engin_mu_del, {id:muid});
    }
    function auth_but_func_showjia(muid){
        openPage('showmu.php?id='+muid, '1000px', '690px');
    }





    function auth_but_func_addbao(buttitle){
        layui.use(['laytp', 'form'], function() {
            var form = layui.form, layer = layui.layer;
            var muid = $("#muid").val() || 0;
            facade.ajax({type: 'post', url: ask_engin_bao_add, data:{enginid: param_enginid,muid:muid,yanbot:1}}).done(function(res){
                if (res.code === 2000) {
                    document.getElementById('uploadfile').value = null;
                    $("#uploadfile").click();
                }
            });
        });
    }
    function auth_but_func_openbao(ext,encpath){
        get_yulan_bao_file(ext, encpath)
    }
    function auth_but_func_downbao(encpath){
        public_down_bao_file(encpath)
    }
    function auth_but_func_editbao(id){
        var muid = $("#muid").val();
        openPageEdit('../engin_bao/edittwo.php?close=1&id='+id+'&muid='+muid, '1000px', '690px');
    }
    function auth_but_func_delbao(id){
        data_delete_cur(ask_engin_bao_del, {id:id});
    }
    function auth_but_func_showbao(id){
        openPage('../engin_bao/show.php?id='+id, '1000px', '690px');
    }



    if(typeof(viewerObj) === "undefined" || typeof(viewerObj) === undefined || viewerObj == null){
        var viewerObj = [];
    }
    function get_yulan_bao_file(ext, encpath){
        if(ext == 'png' || ext == 'pneg' || ext == 'jpg' || ext == 'jpeg' || ext == 'gif'){
            var imgElement = document.getElementById('nanimgs');
            imgElement.src = get_bao_file_complete(encpath);
            imgElement.src = imgElement.src + '?timestamp=' + new Date().getTime();
            upload_files_file_viewer('nanimgs');
            $("#nanimgs").click();
        }else if(ext == 'pdf'){
            upload_files_viewer_pdf_show(get_bao_file_complete(encpath))
        }else if(ext == 'doc' || ext == 'docx' || ext == 'xls' || ext == 'xlsx' || ext == 'ppt' || ext == 'pptx'){
            upload_files_vieweroffice_show(get_bao_file_complete(encpath))
        }else{
            upload_files_show_yulan_not();
        }
    }


















    function TableSearchFunc() {
        var muid = $("#muid").val();
        get_jia_info(muid, 1)
    }
    function listTableResetFunc() {
        var name = $("#name").val('');
        var created_user_name = $("#created_user_name").val('');
        var created_at = $("#created_at").val('');

        var muid = $("#muid").val();
        get_jia_info(muid)
    }




    function layer_close(){
        try {
            parent.layui.table.reload('datalist');
        }catch (e) {

        }
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
    function data_delete_cur(url, data, tit='', exp='', width='', height=''){
        if(!tit){
            tit = '是否确认删除？';
        }
        if(!exp){
            exp = '删除后不可恢复，请谨慎删除';
        }
        if(!width){
            width = '450px';
        }
        if(!height){
            height = '220px';
        }
        layer.confirm("<div class='tit'>"+tit+"</div><div class='exp'>"+exp+"</div>", {
                title:'操作确认',
                icon: 3,
                area:[width,height],
                btn:['确定']
            }, function(conindex){
                facade.ajax({url: url, data:data}).done(function(res){
                    if (res.code === 2000) {
                        facade.success("提交成功!");
                        var muid = $("#muid").val() || 0;
                        get_mu_info(muid);
                        get_jia_info(muid);
                        layer.close(conindex);
                    }
                });
            }
        )
    }
</script>
</body>
</html>
